<template>
  <!-- 安全制度管理 -->
  <base-breadcrumb>
    <searchData
      :searchData="searchOptions"
      @search="handleSearch"
    />
    <div class="page-body">
      <a-card :bordered="false">
        <!-- <title-name title="安全制度管理发布" /> -->
        <div class="table-operateors mb20">
          <a-button
            icon="plus"
            type="primary"
            @click="handleUpdate('add')"
            v-btnPermission="'hse_005_add_01'"
          >新增上报</a-button>
        </div>
        <a-alert
          class="mb12"
          type="info"
          show-icon
        >
          <template slot="message">
            <span>安全管理制度总数：<a>{{ countFrom ? countFrom.totalNum : 0 }}</a>个， 待提交审核：<a>{{ countFrom ? countFrom.draftNum : 0 }}</a>个， 审核中：<a>{{ countFrom ? countFrom.submittedNum : 0 }}</a>个，
              审核通过：<a>{{ countFrom ? countFrom.paseNum : 0 }}</a>个， 审核不通过：<a>{{ countFrom ? countFrom.rejectedNum : 0 }}</a>个
            </span>
          </template>
        </a-alert>
        <list-table
          ref="table"
          size="default"
          rowKey="id"
          :columns="columns"
          :request="loadData"
          :alert="true"
          show-size-changer
          show-quick-jumper
          showPagination="auto"
        >
          <span
            slot="sort"
            slot-scope="text, record, index"
          >{{ index + 1 }}</span>
          <!-- <template slot="status" slot-scope="text, record">
            <StatusDisplay :status-code="record.status" />
          </template> -->
          <span
            slot="status"
            slot-scope="text, record"
          >
            <list-status
              :status="record.status"
              :isApproval="true"
              conversion="en"
              :text="text"
            ></list-status>
          </span>
          <template
            slot="operator"
            slot-scope="text, record"
          >
            <MoreButton>
              <a
                class="base-btn"
                @click="handleUpdate('look', record)"
                v-btnPermission="'hse_005_loo_04'"
              >查看</a>

              <a
                class="base-btn"
                @click="handleUpdate('edit', record)"
                v-if="(record.status === 'DRAFT' || record.status === 'REJECTED')&&selfBtnShow(record.creatorId)"
                v-btnPermission="'hse_005_edi_02'"
              >编辑</a>

              <a
                class="base-btn"
                v-if="record.status == 'PAST'&&hideBuquBtn()"
                @click="handleUpdate('del', record)"
                v-btnPermission="'hse_005_moi_06'"
              >撤销</a>

              <a
                v-if="record.status && record.status !== 'DRAFT'"
                @click="toggleApprovalDetails(record)"
                v-btnPermission="'hse_005_moi_05'"
              >审批信息</a>
            </MoreButton>
          </template>
        </list-table>
      </a-card>
    </div>
    <ApprovalDetails
      :visible="visible.approvalDetails"
      :businessId="childInitData.id"
      @cancel="visible.approvalDetails = false"
      @confirm="visible.approvalDetails = false"
    />
  </base-breadcrumb>
</template>

<script>
import ApprovalDetails from '@/components/approvalDetails'
import { getSystemClassifyList, deleteSystemManage, getSystemManageList, GetCount } from '@/api/safetySupervision/safetySystem.js'
import StatusDisplay from '@/components/StatusDisplay'
import { STable } from '@/components'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import OrgTreeSelect from '@/components/OrgTreeSelect'
import Vue from 'vue'
const columns = [
  {
    title: '序号',
    dataIndex: 'sort',
    scopedSlots: { customRender: 'sort' }
  },
  {
    title: '制度名称',
    dataIndex: 'name'
  },
  // {
  //   title: '制度类型',
  //   dataIndex: 'sysTypeName',
  // },
  {
    title: '所属分公司',
    dataIndex: 'branchComName'
  },
  {
    title: '所属项目',
    dataIndex: 'projectName'
  },
  {
    title: '填报人',
    dataIndex: 'createName'
  },
  {
    title: '填报时间',
    dataIndex: 'createTime'
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: ' 操作',
    dataIndex: 'operator',
    scopedSlots: { customRender: 'operator' }
  }
]
const searchOptions = [
  {
    name: '所属单位',
    valueKey: 'searchQuery',
    type: OrgTreeSelect,
  },
  // {
  //   name: '所属分公司',
  //   valueKey: 'branchComId',
  //   type: SelectSubCompany
  // },
  // {
  //   name: '所属项目',
  //   keyValue: 'branchComId',
  //   valueKey: 'projectId',
  //   type: SelectProject
  // },
  {
    name: '制度类型',
    value: 'sysTypeId',
    type: 'select'
  },
  {
    name: '制度名称',
    value: 'name',
    type: 'input'
  },
  {
    name: '编制人',
    value: 'redact',
    type: 'input'
  },
  {
    name: '填报人',
    value: 'createName',
    type: 'input'
  },
  {
    name: '填报时间',
    value: 'applyTime',
    type: 'dateRange'
  },
  {
    name: '状态',
    value: 'status',
    type: 'select',
    data: typeListApproval.filter((el, index) => index < typeListApproval.length - 1)
  }
]
import { typeListApproval } from '@/utils/util'
export default {
  name: 'a' + Date.now(),
  components: {
    STable,
    StatusDisplay,
    ApprovalDetails
  },
  data() {
    return {
      childInitData: {},
      countFrom: {},
      visible: {
        approvalDetails: false
      },
      onlyCompany: 1,
      searchOptions: searchOptions,
      queryParam: {},
      columns: columns,
      // loadData: async (parameter) => {
      //     const requestParameters = Object.assign({}, parameter, this.queryParam)
      //     const [err, res] = await getSystemManageList(requestParameters)
      //     this.handleCount(this.queryParam)
      //     return new Promise((resolve, reject) => {
      //         if (err) {
      //             reject(err)
      //         } else {
      //             resolve(res)
      //         }
      //     })
      // },
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam )
        this.handleCount(this.queryParam)
        setTimeout(() => {
          this.$forceUpdate()
        }, 300)
        return getSystemManageList(requestParameters).then((res) => {
          console.log('res', res)
          return res[1]
        })
      }
    }
  },
  created() {
    this.getSystemClassifyList()
  },
  methods: {
    toggleApprovalDetails(data) {
      this.childInitData = { ...data }
      this.visible.approvalDetails = true
    },
    async getSystemClassifyList() {
      const [err, res] = await getSystemClassifyList()
      if (!err) {
        const classifyList = (res.data || [])
          .filter((item) => {
            return !!item.enabled
          })
          .map((item) => {
            return {
              label: item.name,
              value: item.id
            }
          })
        this.searchOptions = this.searchOptions.map((item) => {
          return item.value === 'sysTypeId' ? { ...item, data: classifyList } : item
        })
      }
    },
    handleSearch(val = {}) {
      this.queryParam = {
        ...val,
        createTimeBegin: (val.applyTime && val.applyTime.length && val.applyTime[0].format('YYYY-MM-DD')) || void 0,
        createTimeEnd: (val.applyTime && val.applyTime.length && val.applyTime[1].format('YYYY-MM-DD')) || void 0
      }
      this.$refs.table.refresh(true)
      this.handleCount(this.queryParam)
    },
    //撤销操作
    async deleteSystemManage(data) {
      const [err, res] = await deleteSystemManage(data)
      if (!err) {
        this.$message.success('成功')
        this.$refs.table.refresh(true)
      }
    },

    // 处理更新操作（新增、编辑）
    async handleUpdate(type, data = {}) {
      if (type == 'del') {
        let that = this
        this.$confirm({
          title: '温馨提示',
          content: '确定撤销？',
          onOk: () => {
            return that.deleteSystemManage(data)
          }
        })
      } else {
        this.$router.push({
          name: 'UpdateSystem',
          query: {
            id: data.id || '',
            pageType: type
          }
        })
      }
    },

    //获取统计
    async handleCount(queryParam) {
      const [err, res] = await GetCount(queryParam)
      if (!err) {
        this.countFrom = res.data
      }
    }
  },
  mounted() {
    this.handleCount(this.queryParam)
  }
}
</script>

<style lang="less" scoped>
.page-header {
  .header-row {
    display: flex;
    .flex-content {
      flex: auto;
    }
    .divider {
      margin: 0 12px;
    }
    .expand-btn {
      margin-left: 8px;
      font-size: 14px;
    }
  }
}
.page-body {
  .table-operateors {
    .ant-btn + .ant-btn {
      margin-left: 12px;
    }
  }
}
</style>
